package com.cg.service.impl;

import cn.dev33.satoken.stp.SaTokenInfo;
import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cg.dto.LoginDTO;
import com.cg.entity.User;
import com.cg.mapper.UserMapper;
import com.cg.result.R;
import com.cg.service.AccountService;
import com.cg.service.IUserService;
import com.cg.utils.Md5Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;


/**
 * <p>
 * 基础表-省市区 服务实现类
 * </p>
 *
 * @author 加
 * @since 2024-12-26
 */
@Service
public class AccountServiceImpl extends ServiceImpl<UserMapper, User> implements AccountService {

    @Autowired
    private IUserService userService;
    @Override
    public R login(LoginDTO loginDTO) {
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(User::getAccount, loginDTO.getAccount());
        User user = this.getOne(queryWrapper);
        if(!(user.getPassword()).equals(Md5Utils.hash(loginDTO.getPassword()))){
            return R.Failed("密码错误，请重新输入");
        }
        if (loginDTO.getAccount()== null) {
            return R.Failed("用户不存在，请重新输入");
        }
        //密码正确返回token
        if (user.getStatus() == 1) {
            return R.Failed("账号已禁用，无法登录");
        }
        else if (user.getStatus() == 0) {
            // 第1步，先登录上
            StpUtil.login(loginDTO.getAccount());
            // 第2步，获取 Token  相关参数
            SaTokenInfo tokenInfo = StpUtil.getTokenInfo();
            // 第3步，返回给前端
            Map resultMap = new HashMap<>();
            resultMap.put("token", tokenInfo);
            return R.Success(resultMap);
        }
        else{
            return R.Failed("人员状态异常，登录失败");
        }
    }

    @Override
    public R<?> getCurrentUser() {
        //1、查询当前登录状态
        boolean isLogin = StpUtil.isLogin();
        if(!isLogin){
            return R.Failed("当前客户端未登录");
        }
        //2、获取当前登录用户的id
        String userId = StpUtil.getLoginIdAsString();
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(User::getAccount,userId);
        User user = userService.getOne(queryWrapper);
        Map<String, Object> result = new HashMap<>();
        SaTokenInfo tokenInfo = StpUtil.getTokenInfo();
        result.put("token", tokenInfo.getTokenValue());
        result.put("user", user);
        //3、返回给前端
        return R.Success(result);
    }

    @Override
    public Long getCurrentUserId() {
        //1、查询当前登录状态
        boolean isLogin = StpUtil.isLogin();
        if(!isLogin){
            return null;
        }
        //2、获取当前登录用户的id
        String userId = StpUtil.getLoginIdAsString();
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(User::getAccount,userId);
        User user = userService.getOne(queryWrapper);
        //3、返回给前端
        return user.getUserId();
    }

//    @Override
//    public List<User> getCurrentUser(String loginId) {
//        LambdaQueryWrapper<User> queryWrapper=new LambdaQueryWrapper<>();
//        queryWrapper.eq(loginId!=null,User::getAccount,loginId);
//
//        List<User> list=list(queryWrapper);
//        return list;
//    }

    @Override
    public R logout() {
        // 1. 尝试根据用户名登出（通常是根据当前会话，但这里为了示例完整性，我们仍然传入用户名）
        StpUtil.logout();
        // 登出成功，返回成功信息
        return R.Success("登出成功");
    }

}